function Banner() {
	this.bannerGroup = $('#banner-group');
	this.bannerUl = $('#banner-ul');
	this.index = 1;
	this.timer = null;
	this.bannerLength = $('#banner-ul').children('li').length;
	this.bannerWidth = 798;
	this.ctl = $('.window-control');
}

Banner.prototype.animate = function () {
	this.bannerUl.stop(true,true).animate({left: -798*this.index}, 500);
	var index = this.index;
	if (index === 0) {
		index = this.bannerLength-1;
	}else if(index == this.bannerLength+1) {
		index = 0;
	}else {
		index = this.index-1;
	}
	this.allLis.eq(index).addClass('current').siblings().removeClass('current');
}

Banner.prototype.listenBannerAction = function () {
	var self = this;
	this.timer = setInterval(function () {
		if (self.index >= self.bannerLength+1) {
			self.index = 2;
			self.bannerUl.css({'left': -self.bannerWidth})
		} else {
			self.index++;
		}
		self.animate();
	}, 2000);
}

Banner.prototype.listenBannerCtlClickEvent = function () {
	var self = this;
	var prev = $('.prev-ctl');
	var next = $('.next-ctl');
	prev.click(function () {
		if (self.index <= 0) {
			self.bannerUl.css({'left': -self.bannerWidth*self.bannerLength})
			self.index = self.bannerLength-1;
		} else {
			self.index--;
		};
		self.animate();
	})
	next.click(function () {
		if (self.index >= self.bannerLength+1) {
			self.bannerUl.css({'left': -self.bannerWidth});
			self.index = 2;
		} else {
			self.index++;
		};
		self.animate();
	})
}

Banner.prototype.listenHandelPageCtl = function () {
	var pageCtl = this.bannerGroup.find('.page-control');
	for (var i = 0; i<this.bannerLength; i++) {
		var li = document.createElement('li');
		if (i==0) $(li).addClass('current');
		pageCtl[0].appendChild(li);
	}
}

Banner.prototype.listenPageControlClickEvent = function () {
	var self = this;
	this.allLis = $('#page-control').children();
	this.allLis.each(function (index, obj) {
		$(obj).click(function () {
			self.index = index+1;
			self.animate();
		})
	})
}

Banner.prototype.listenBannerHover = function () {
	var self = this;
	this.bannerGroup.hover(function () {
		clearInterval(self.timer);
	} ,function () {
		self.listenBannerAction();
	})
}

Banner.prototype.initBanner = function () {
	var first = this.bannerUl.children().eq(0).clone();
	var end = this.bannerUl.children().eq(this.bannerLength-1).clone();
	this.bannerUl.append(first);
	this.bannerUl.prepend(end);
	this.bannerUl.css({
		'width': (this.bannerLength+2)*798,
		'left': -798
	});
}

Banner.prototype.run = function () {
	clearInterval(this.timer);
	this.initBanner();
	this.listenHandelPageCtl();
	this.listenPageControlClickEvent();
	this.listenBannerAction();
	this.listenBannerHover();
	this.listenBannerCtlClickEvent();
	this.HandelHeader();
	this.PageSilder();
}

Banner.prototype.HandelHeader = function () {
	var self = this;
	var header = $('#header');
	window.onscroll = function () {
		var t = document.documentElement.scrollTop || document.body.scrollTop;
		if (t>=76) {
			header.css({'opacity': 0.5});
			self.ctl.show();
		} else {
			header.css({'opacity': 1});
			self.ctl.hide();
		}
	}
}

Banner.prototype.PageSilder = function () {
	var toTop = this.ctl.find('li').eq(0);
	var toBottom = this.ctl.find('li').eq(1);
	toTop.on("click", function() {
      	$('body,html').animate({scrollTop: 0}, 500);
		return false;
     });
	toBottom.on("click", function () {
		var bottom = $(document).height();
		$('body,html').animate({scrollTop: bottom}, 500);
	    return false;
	})
}

$(function () {
	var banner = new Banner();
	banner.run();
})